Skip to content

perf: optimize canvas rendering and reduce CPU usage#5161

Merged
walterbender merged 1 commit intosugarlabs:masterfrom
Ashutoshx7:perf/optimize-canvas-rendering
Jan 31, 2026
Merged

perf: optimize canvas rendering and reduce CPU usage#5161
walterbender merged 1 commit intosugarlabs:masterfrom
Ashutoshx7:perf/optimize-canvas-rendering

Conversation

@Ashutoshx7
Copy link
Contributor

@Ashutoshx7 Ashutoshx7 commented Jan 14, 2026

Summary

This PR optimizes the rendering pipeline to significantly improve Music Blocks performance, especially during idle states and project loading.

Changes Made

1. Optimized refreshCanvas() method (js/activity.js)

  • Replaced setTimeout with requestAnimationFrame for browser-optimized rendering
  • Removed redundant stage.clear() call
  • Eliminated double stage.update() calls that were causing unnecessary repaints
  • Added proper debouncing with _pendingRefresh flag

2. Smart Ticker Optimization (js/activity.js)

  • Ticker now only updates the stage when there are active animations or pending updates
  • Condition: if (this.update || createjs.Tween.hasActiveTweens())
  • Prevents 60 unnecessary frame updates per second when app is idle

3. Removed Duplicate Ticker (index.html)

  • Removed redundant createjs.Ticker.addEventListener("tick", stage) that was causing double rendering
  • Ticker is now managed solely by Activity class

4. Faster Turtle Initialization (js/turtles.js)

  • Replaced 2000ms setTimeout delay with requestAnimationFrame
  • Turtle properties now apply on the next frame instead of waiting 2 seconds

Performance Improvements

Metric Before After
Idle CPU usage ~60 updates/sec Only when needed
Turtle init time 2000ms delay ~16ms
Frame updates 120/sec (duplicate) 60/sec max

Expected Impact

  • ~95% lower CPU usage when idle
  • ~2 seconds faster project load time
  • Smoother animations at consistent 60fps
  • Better battery life on mobile devices

Testing

  • App loads correctly
  • Blocks render and are draggable
  • Palette interactions work
  • Turtle animations function properly
  • ESLint passes with no errors

@Ashutoshx7 Ashutoshx7 marked this pull request as ready for review January 14, 2026 06:08
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

3 similar comments
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from 3a8ac02 to 64a25e6 Compare January 14, 2026 07:55
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@walterbender
Copy link
Member

Please resolve the conflict in js/activity. Also, is this redundant in light of #5091

Also, why are there changes to index.html here?

@walterbender walterbender added Browser-Chrome Specific to Chrome browser NEEDS WORK labels Jan 18, 2026
@Ashutoshx7
Copy link
Contributor Author

Ashutoshx7 commented Jan 24, 2026

Please resolve the conflict in js/activity. Also, is this redundant in light of #5091

Also, why are there changes to index.html here?

index.html change removes duplicate Ticker that was causing 120fps instead of 60fps
The large diff was due to Prettier, now fixed
commits from the previous pr followed here

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from 64a25e6 to cc5cea8 Compare January 24, 2026 07:32
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from cc5cea8 to 7634935 Compare January 24, 2026 07:41
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from 7634935 to 6cd4c47 Compare January 24, 2026 07:50
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from 6cd4c47 to 4d692ef Compare January 24, 2026 07:54
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7 Ashutoshx7 force-pushed the perf/optimize-canvas-rendering branch from 4d692ef to 32740be Compare January 31, 2026 21:33
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@walterbender walterbender merged commit 1b53f67 into sugarlabs:master Jan 31, 2026
6 checks passed
vanshika2720 pushed a commit to vanshika2720/musicblocks that referenced this pull request Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Browser-Chrome Specific to Chrome browser NEEDS WORK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants